/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbCloud			*/
/*	Author:		gong_libin			*/
/*	Date:		2012_01_09			*/
/*	File:		MrTime.cpp			*/
/********************************************************/

#include "MrTime.h"

CMrTime::CMrTime()
{
	memset(&m_stOn, '\0', sizeof(struct timeval));
	memset(&m_stOff, '\0', sizeof(struct timeval));
}

CMrTime::~CMrTime()
{
}

void CMrTime::MrTimeOn()
{
	gettimeofday(&m_stOn, NULL);

	return;
}

void CMrTime::MrTimeOff()
{
	gettimeofday(&m_stOff, NULL);

	return;
}

void* CMrTime::MrTimeGetOn()
{
	return (void*)&m_stOn;
}

void* CMrTime::MrTimeGetOff()
{
	return (void*)&m_stOff;
}

void CMrTime::MrTimeDisplay()
{
	if (m_stOff.tv_usec == m_stOn.tv_usec) {
		MR_PRINT("Second: %ld MicroSecond: 0\n", m_stOff.tv_sec - m_stOn.tv_sec);
	}
	else if (m_stOff.tv_usec > m_stOn.tv_usec) {
		MR_PRINT("Second: %ld MicroSecond: %ld\n", m_stOff.tv_sec - m_stOn.tv_sec, m_stOff.tv_usec - m_stOn.tv_usec);
	}
	else {
		MR_PRINT("Second: %ld MicroSecond: %ld\n", m_stOff.tv_sec - m_stOn.tv_sec - 1, 1000000L - m_stOn.tv_usec + m_stOff.tv_usec);
	}

	return;
}

void CMrTime::MrTimeDisplayOnNumber()
{
	MR_PRINT("Second: %ld MicroSecond: %ld\n", m_stOn.tv_sec, m_stOn.tv_usec);

	return;
}

void CMrTime::MrTimeDisplayOnString()
{
	struct tm* pstTime = NULL;
	char szTime[MR_BUFFER] = { 0 };

	pstTime = localtime(&m_stOn.tv_sec);
	strftime(szTime, MR_BUFFER, "%H:%M:%S", pstTime);
	MR_PRINT("Time: %s.%ld\n", szTime, m_stOn.tv_usec);

	return;
}

void CMrTime::MrTimeDisplayOffNumber()
{
	MR_PRINT("Second: %ld MicroSecond: %ld\n", m_stOff.tv_sec, m_stOff.tv_usec);

	return;
}

void CMrTime::MrTimeDisplayOffString()
{
	struct tm* pstTime = NULL;
	char szTime[MR_BUFFER] = { 0 };

	pstTime = localtime(&m_stOff.tv_sec);
	strftime(szTime, MR_BUFFER, "%H:%M:%S", pstTime);
	MR_PRINT("Time: %s.%ld\n", szTime, m_stOff.tv_usec);

	return;
}
